// pages/drivingroute/drivingroute.js
const app = getApp();
const amapFile = require('../../libs/amap-wx');
const myAmapFun = new amapFile.AMapWX({ key: app.globalData.gd_key });
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 1,
        titleList: [
            {
                id: 1,
                title: '驾车'
            },
            {
                id: 2,
                title: '公交'
            },
            {
                id: 3,
                title: '步行'
            }
        ],
        userLatitude: +wx.getStorageSync('location').latitude,
        userLongitude: +wx.getStorageSync('location').longitude,
        markers: [{
            iconPath: "../../images/dingwei.png",
            id: 0,
            latitude: wx.getStorageSync('location').latitude,
            longitude: wx.getStorageSync('location').longitude,
            width: 24,
            height: 34
        }],
        distance: '',
        cost: '',
        polyline: []
    },
    handleTarget(e1, e2) {
        let target = {
            iconPath: "../../images/dingwei.png",
            id: 1,
            latitude: e2,
            longitude: e1,
            width: 24,
            height: 34
        };
        let arr = this.data.markers;
        arr.push(target);
        this.setData({
            markers: arr
        });
        console.log(this.data.markers);
    },
    whichWay(e) {
        console.log(e);
        this.setData({
            active: e.currentTarget.dataset.id
        });
        wx.showToast({
            icon: 'none',
            title: '此功能暂未开放~'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.handleTarget(options.longitude, options.latitude)
        this.handleDrivingRoute(options.longitude, options.latitude);
    },
    handleDrivingRoute(e1, e2) {
        myAmapFun.getDrivingRoute({
            origin: this.data.userLongitude + ',' + this.data.userLatitude,
            destination: e1 + ',' + e2,
            success: res => {
                console.log(res);
                var points = [];
                if (res.paths && res.paths[0] && res.paths[0].steps) {
                    var steps = res.paths[0].steps;
                    for (var i = 0; i < steps.length; i++) {
                        var poLen = steps[i].polyline.split(';');
                        for (var j = 0; j < poLen.length; j++) {
                            points.push({
                                longitude: parseFloat(poLen[j].split(',')[0]),
                                latitude: parseFloat(poLen[j].split(',')[1])
                            })
                        }
                    }
                }
                console.log(points)
                this.setData({
                    cost: res.taxi_cost,
                    distance: res.paths[0].distance,
                    polyline: [{
                        points: points,
                        color: "#0091ff",
                        width: 6
                    }]
                });
            },

            fail: res => {
                console.log(res);
            },
            complete: res => {
                console.log(res)
            }
        });
        // myAmapFun.getRegeo()
        // console.log(myAmapFun.getRegeo());
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})